package com;

/**
 * 非线程安全测试
 * 无synchronized关键字则打印结果a的线程错误
 */

public class Demo01 {
    public static void main(String[] args) {
        Alogin Alogin = new Alogin();
        Alogin.start();
        Blogin blogin = new Blogin();
        blogin.start();
    }
}

class Login {
    private static String nameRef;
    private static String passwordRef;

    public static void dopost(String name, String password) {
        nameRef = name;
        if (name == "a") {
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        passwordRef = password;
        System.out.println("username:" + nameRef + ",password:" + passwordRef);
    }
}

class Alogin extends Thread {

    @Override
    public void run() {
        Login.dopost("a", "aa");
    }
}

class Blogin extends Thread {

    @Override
    public void run() {
        Login.dopost("b", "bb");
    }
}
